package com.lagou.mr.sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

//因为这个类的实例对象要作为map输出的key，所以要实现writablecomparalbe接口

public class OrderBean implements WritableComparable<OrderBean> {


    private Integer order;
    //定义属性
    private Long num;


    public Long getNum() {
        return num;
    }

    public void setNum(Long num) {
        this.num = num;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    //序列化方法
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(num);

    }

    //反序列化方法
    @Override
    public void readFields(DataInput in) throws IOException {
        this.num = in.readLong();
    }

    //指定排序规则,我们希望按照总时长进行排序
    @Override
    public int compareTo(OrderBean o) {  //返回值三种：0：相等 1：小于 -1：大于
        System.out.println("compareTo 方法执行了。。。");
        //指定按照bean对象的总时长字段的值进行比较
        if (this.num > o.num) {
            return 1;
        } else if (this.num < o.num) {
            return -1;
        } else {
            return 0; //加入第二个判断条件，二次排序
        }

    }

    @Override
    public boolean equals(Object o) {
        System.out.println("equals方法执行了。。。");
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return Objects.hash(getOrder(), getNum());
    }

    @Override
    public String toString() {
        return order + "  " + num;
    }

//    @Override
//    public String toString() {
//        return "OrderBean{" +
//                "order=" + order +
//                ", num=" + num +
//                '}';
//    }
}
